package org.myorg.datastraemapi.demo;

import org.apache.flink.api.common.functions.FoldFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.myorg.datastraemapi.model.Student;

import java.util.ArrayList;
import java.util.List;

public class FlodDemo {

	public static void main(String[] args) throws Exception {
		final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

		List<Student> listSource = new ArrayList<Student>() {{
			add(new Student(1, 1));
			add(new Student(2, 2));
			add(new Student(3, 3));
			add(new Student(4, 4));
			add(new Student(5, 4));
			add(new Student(6, 4));
			add(new Student(7, 4));
		}};
		DataStreamSource<Student> dataStreamSource = env.fromCollection(listSource);


		KeyedStream<Student, Integer> keyBy = dataStreamSource.keyBy(new KeySelector<Student, Integer>() {
			@Override
			public Integer getKey(Student student) throws Exception {
				return student.age;
			}
		});
		//		keyBy.print();

		SingleOutputStreamOperator<String> flod = keyBy.fold("idlist:", new FoldFunction<Student, String>() {
			@Override
			public String fold(String s, Student o) throws Exception {
				s += o.id + "-";
				return s;
			}
		});
		//		flod.print();

		env.execute("Flink Streaming Java API Skeleton");

	}

}
